﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;
using BlogEngine.Core;
using System.Globalization;

namespace AzureProvider.SoupMethods
{
    public class PostsMethods : BaseMethods
    {
        protected override string containerId
        {
            get { return "Posts"; }
        }

        protected override string kind
        {
            get { return "Post"; ; }
        }

        protected override Entity CreateEntity(object Object)
        {
            Post myobj = (Post)Object;

            var entity = new Entity()
            {
                Id = myobj.Id.ToString(),
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("Title", myobj.Title);
            entity.Properties.Add("Description", myobj.Description);
            entity.Properties.Add("Content", myobj.Content);

            entity.Properties.Add("DateCreated",
                myobj.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss",
                CultureInfo.InvariantCulture));
            entity.Properties.Add("DateModified",
                myobj.DateModified.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss",
                CultureInfo.InvariantCulture));

            entity.Properties.Add("Author", myobj.Author);
            entity.Properties.Add("IsPublished", myobj.IsPublished);
            entity.Properties.Add("IsCommentsEnabled", myobj.IsCommentsEnabled);
            entity.Properties.Add("Raters", myobj.Raters.ToString());

            entity.Properties.Add("Rating", myobj.Rating.ToString());
            entity.Properties.Add("Slug", myobj.Slug);            

            return entity;

        }

        public override object CreateObject(Entity Entity)
        {
            Post myobj = new Post
            {
                Id = new Guid(Entity.Id),
                Title = (string)Entity.Properties.Single(tt => tt.Key == "Title").Value,
                Description = (string)Entity.Properties.Single(tt => tt.Key == "Description").Value,
                Content = (string)Entity.Properties.Single(tt => tt.Key == "Content").Value,

                DateCreated = DateTime.Parse((string)Entity.Properties.Single(tt => tt.Key == "DateCreated").Value),
                DateModified = DateTime.Parse((string)Entity.Properties.Single(tt => tt.Key == "DateModified").Value),

                Author = (string)Entity.Properties.Single(tt => tt.Key == "Author").Value,
                IsPublished = (bool)Entity.Properties.Single(tt => tt.Key == "IsPublished").Value,
                IsCommentsEnabled = (bool)Entity.Properties.Single(tt => tt.Key == "IsCommentsEnabled").Value,

                Raters = int.Parse((string)Entity.Properties.Single(tt => tt.Key == "Raters").Value),                
                Rating = float.Parse((string)Entity.Properties.Single(tt => tt.Key == "Rating").Value),
                Slug = (string)Entity.Properties.Single(tt => tt.Key == "Slug").Value                

            };

            return myobj;

        }


        public IEnumerable<Post> GetPosts(string query, out string errMessage)
        {
            return base.GetObjects(query, out errMessage).Cast<Post>();
        }

        public void FillRelated(ref Post post,out string  errMessage)
        {
            PostTagMethods PostTagMethods = new PostTagMethods();
            post.Tags.AddRange(PostTagMethods.GetPostTags(post.Id.ToString(), out errMessage).Select(t => t.Tag).ToArray());
            Post pt = post;
            PostCommentMethods PostCommentMethods = new PostCommentMethods();
            post.Comments.AddRange(PostCommentMethods.GetPostComments(post.Id.ToString(), out errMessage).ToArray());
            post.Comments.Sort();

            PostCategoryMethods PostCategoryMethods = new PostCategoryMethods();
            post.Categories.AddRange(PostCategoryMethods.GetPostCategories(post.Id.ToString(), out errMessage)
                .Select(t => Category.GetCategory((t.CategoryId))).ToArray());

            PostNotifyMethods PostNotifyMethods = new PostNotifyMethods();
            post.NotificationEmails.AddRange(PostNotifyMethods.GetPostNotifies(post.Id.ToString(), out errMessage)
                .Select(t => t.NotifyAddress).ToArray());
            //pour retrouver le parent du comment
            post.Comments.ForEach(act => act.Parent = pt);
            
        }

        public void DeletePost(string Id, out string errMessage)
        {
            errMessage = null;
            Delete(Id);

            PostCategoryMethods PostCategoryMethods = new PostCategoryMethods();
            PostCategoryMethods.DeletePostRelated(Id, out errMessage);

            PostCommentMethods PostCommentMethods = new PostCommentMethods();
            PostCommentMethods.DeletePostRelated(Id, out errMessage);

            PostNotifyMethods PostNotifyMethods = new PostNotifyMethods();
            PostNotifyMethods.DeletePostRelated(Id, out errMessage);

            PostTagMethods PostTagMethods = new PostTagMethods();
            PostTagMethods.DeletePostRelated(Id, out errMessage);          
        }
    }
}
